package uk.ac.aber.Blockmation.ColorsTable;

import java.util.Hashtable;
import java.util.Iterator;

import javax.swing.table.AbstractTableModel;

import uk.ac.aber.Blockmation.OurColors;

/**
 * The model to fill a table with data used by ColorsTable class.
 * The data about colors comes from OurColors class. 
 * 
 * <br />
 * This model produces a table with 4 columns. Each cell of the table 
 * gets data off OurColors class and stores them as Color object.
 *  
 * @author Aron
 *
 */
class OurColorsTableModel extends AbstractTableModel {
	
	/** Data about colors to put in the table */
    private OurColors ourColors = new OurColors();
    private int noOfColors = ourColors.getLetters().size();
    private Object[][] data = new Object[noOfColors/4][4];

    private String[] columnNames = {"1",
                                    "2",
                                    "3",
                                    "4"};
    /**
     * This constructor performs filling each cell with Color object 
     * that comes from OurColor class hashtable.
     * 
     */
    public OurColorsTableModel()
    {
        Hashtable colors = ourColors.getLetters();

        // Iterate over all values in table 
        Iterator it = colors.values().iterator();

        for (int i=0; i < noOfColors/4; i++)
        {
            for (int j=0; j < 4; j++)
            {
                if (it.hasNext()) {
                        // Get value
                        Object value = it.next();
                        data[i][j] = value;
                }
            }
        }
    }

    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.length;
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

}
